package com.sanploy.manage.service.impl;


import com.sanploy.common.domain.Order;
import com.sanploy.common.domain.vo.ChartSalesVo;
import com.sanploy.common.util.CommonUtil;
import com.sanploy.manage.mapper.ChartMapper;
import com.sanploy.manage.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created by zhu yingzhi on 2017/10/18.
 * @author yingzhi zhu
 * 图表逻辑处理接口实现类
 */
@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    private ChartMapper chartMapper;

    //TODO 优化这部分
    @Override
    public List<ChartSalesVo> getRecentOrderSales(int day) {
        List<Order> orders = chartMapper.getRecentOrderSales(day);
        List<ChartSalesVo> list = CommonUtil.getRecentDay(day);
        //遍历设置价格
        list.forEach(item->{
            double price = 0;
            int number = 0;
            for (Order ordersItem:orders){
                System.out.println(Optional.ofNullable(ordersItem.getAdd_time()).orElse(""));
                if (Optional.ofNullable(ordersItem.getAdd_time()).orElse("").contains(item.getTime())){
                    price=price + ordersItem.getOrder_price();
                    number++;
                }
            }
            item.setNumber(number);
            item.setSales(String.valueOf(price));
        });
        return list;
    }
}
